package thread.pool;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author bc
 * @Description:
 * @date 2024/03/13
 */
public class ThreadPoolTest {

    public static void main(String[] args) throws InterruptedException {
        test();
    }

    private static void test() {

        RejectedExecutionHandler rejectionHandler = new CustomRejectedExecutionHandler();

        ThreadPoolExecutor executor = new ThreadPoolExecutor(4, 20, 60000,  TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1));
        executor.setRejectedExecutionHandler(rejectionHandler);

        Runnable run = () -> {
            try {
                Thread.sleep(1000);
                System.out.println(Thread.currentThread().getName() + "正在执行" + LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            executor.execute(run);
        }

        while (!executor.isTerminated()) {
            System.out.println("当前活动线程数: " + executor.getActiveCount());
            System.out.println("当前线程池大小: " + executor.getPoolSize());
            System.out.println("当前队列中排队的任务数: " + executor.getQueue().size());
            try {
                Thread.sleep(1000); // 每隔1秒检查一次线程池状态
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }


        executor.shutdown();
    }

    private static void runPool() {
        ExecutorService service = new ThreadPoolExecutor(5, 20, 3000, TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(5));
        Runnable run = () -> {
            try {
                Thread.sleep(5000);
                System.out.println(Thread.currentThread().getName() + "正在执行" + LocalDateTime.now());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        };

        for (int i = 0; i < 10; i++) {
            System.out.println(i + ":" + LocalDateTime.now());
            service.execute(run);
        }

        service.shutdown();
    }

    private static void taskPool() {
        ExecutorService executor = new ThreadPoolExecutor(5, 20, 50000, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(1));
        List<Future<String>> futures = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            System.out.println(i + ": " + LocalDateTime.now());
            Future<String> future = executor.submit(new MyFutureTask(i + "", 6100L));
            futures.add(future);
        }

        for (Future<String> future : futures) {
            try {
                // 获取每个任务的执行结果，设置超时时间
                String result = future.get(1, TimeUnit.SECONDS);
                System.out.println("任务执行结果：" + result);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            } catch (TimeoutException e) {
                // 超时处理
                future.cancel(true); // 取消任务
                System.out.println("任务超时，已取消");
            }
        }
        executor.shutdown();
    }

}
